//
//  CaculateMD5.m
//  SWCommonFrame
//
//  Created by shaowei on 13-6-17.
//  Copyright (c) 2013年 shaowei. All rights reserved.
//

#import "CaculateMD5.h"
#import <CommonCrypto/CommonDigest.h>

@implementation CaculateMD5

+(NSString *)calculateMD5WithString:(NSString *)str{
    const char *cStr = [str UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr,strlen(cStr),digest);
    NSString* MD5Str = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                        digest[0], digest[1],
                        digest[2], digest[3],
                        digest[4], digest[5],
                        digest[6], digest[7],
                        digest[8], digest[9],
                        digest[10], digest[11],
                        digest[12], digest[13],
                        digest[14], digest[15]];
    return MD5Str;
}

+(NSString *)calculateMD5WithFileData:(NSData *)fileData{
    CC_MD5_CTX md5;
    CC_MD5_Init(&md5);
    CC_MD5_Update(&md5, [fileData bytes], [fileData length]);
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5_Final(digest, &md5);
    NSString* MD5Str = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                        digest[0], digest[1],
                        digest[2], digest[3],
                        digest[4], digest[5],
                        digest[6], digest[7],
                        digest[8], digest[9],
                        digest[10], digest[11],
                        digest[12], digest[13],
                        digest[14], digest[15]];
    return MD5Str;
}

+(NSString *)calculateMD5WithFilePath:(NSString *)filePath{
    NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
    CC_MD5_CTX md5;
    CC_MD5_Init(&md5);
    NSData *fileData;
    do {
        fileData = [handle readDataOfLength: 256 ];
        CC_MD5_Update(&md5, [fileData bytes], [fileData length]);
    } while ([fileData length]!=0);
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5_Final(digest, &md5);
    NSString* MD5Str = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                        digest[0], digest[1],
                        digest[2], digest[3],
                        digest[4], digest[5],
                        digest[6], digest[7],
                        digest[8], digest[9],
                        digest[10], digest[11],
                        digest[12], digest[13],
                        digest[14], digest[15]];
    return MD5Str;
}

+(NSString *)calcMD5WithFilePath:(NSString *)filePath{
    CC_MD5_CTX md5;
    CC_MD5_Init(&md5);
    NSData *fileData=[NSData dataWithContentsOfFile:filePath];
    CC_MD5_Update(&md5, [fileData bytes], [fileData length]);
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5_Final(digest, &md5);
    NSString* MD5Str = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                        digest[0], digest[1],
                        digest[2], digest[3],
                        digest[4], digest[5],
                        digest[6], digest[7],
                        digest[8], digest[9],
                        digest[10], digest[11],
                        digest[12], digest[13],
                        digest[14], digest[15]];
    
    return MD5Str;
}

+(BOOL)compareWithMD5:(NSString *)MD5 string:(NSString *)str{
    NSString *calculateMD5Str=[self calculateMD5WithString:str];
    return [MD5 isEqualToString:calculateMD5Str];
}

+(BOOL)compareWithMD5:(NSString *)MD5 fileData:(NSData *)fileData{
    NSString *calculateMD5Str=[self calculateMD5WithFileData:fileData];
    return [MD5 isEqualToString:calculateMD5Str];
}

+(BOOL)compareWithMD5:(NSString *)MD5 filePath:(NSString *)filePath{
    NSString *calculateMD5Str=[self calculateMD5WithFilePath:filePath];
    return [MD5 isEqualToString:calculateMD5Str];
}

@end
